import cv2
import os
import sys
from matplotlib import pyplot as plt

if len(sys.argv) <= 1:
    exit(-1)

filename = sys.argv[1]
image = cv2.imread(filename, cv2.IMREAD_GRAYSCALE)
image = cv2.cvtColor(image, cv2.COLOR_GRAY2RGB)
r, g, b = cv2.split(image)
ax1 = plt.subplot(221)
ax1.set_title(os.path.basename(filename))
plt.imshow(image)
plt.xticks([]), plt.yticks([])  # 隐藏坐标轴刻度
# 绘制RGB灰度直方图
ax2 = plt.subplot(222)
ax2.set_title('Red')
plt.hist(r.ravel(), 256, [0, 256])
ax3 = plt.subplot(223)
ax3.set_title('Green')
plt.hist(g.ravel(), 256, [0, 256])
ax4 = plt.subplot(224)
ax4.set_title('Blue')
plt.hist(b.ravel(), 256, [0, 256])
plt.show()
